﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
using System.Drawing;

namespace BHXJDOTNET
{
    /// <summary>
    /// 触摸键盘辅助
    /// </summary>
    public class TabTipHelper
    {
        /// <summary>
        /// The window is disabled. See http://msdn.microsoft.com/en-gb/library/windows/desktop/ms632600(v=vs.85).aspx.
        /// </summary>
        public const UInt32 WS_DISABLED = 0x8000000;

        /// <summary>
        /// Specifies we wish to retrieve window styles.
        /// </summary>
        public const int GWL_STYLE = -16;
        public const int GWL_EXSTYLE = -20;

        private const uint WS_EX_LAYERED = 0x80000;
        private const int WS_EX_TRANSPARENT = 0x20;  

        const int WM_SYSCOMMAND = 0x0112;
        const int SC_CLOSE = 0xF060;
        const int SC_MINIMIZE = 0xF020;
        const int SC_MAXIMIZE = 0xF030;

        //private const Int32 WM_SYSCOMMAND = 274;
        //private const UInt32 SC_CLOSE = 61536;
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern bool PostMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern int RegisterWindowMessage(string lpString);
        [DllImport("user32.dll", SetLastError = true)]
        static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll", SetLastError = true)]
        static extern UInt32 SetWindowLong(IntPtr hWnd, int nIndex,UInt32 dwNewLong);

        [DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
        private static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, int bAlpha, int dwFlags);

        public static void ShowScreenKeyboard()
        {

            if (TabTipHelper.IsKeyboardVisible() != 0x94000000)
            {
                if (File.Exists("C:\\Program Files\\Common Files\\microsoft shared\\ink\\TabTip.exe"))
                {
                    Process process = new Process();
                    process.StartInfo.FileName = "C:\\Program Files\\Common Files\\microsoft shared\\ink\\TabTip.exe";
                    process.Start();
                }
            }
        }
        public static void HideInputPanel()
        {
            IntPtr TouchhWnd = new IntPtr(0);
            TouchhWnd = FindWindow("IPTip_Main_Window", null);
            if (TouchhWnd == IntPtr.Zero)
                return;
            PostMessage(TouchhWnd, WM_SYSCOMMAND, SC_CLOSE, 0);
        }
        public static IntPtr GetKeyboardWindowHandle()
        {
            return FindWindow("IPTip_Main_Window", null);
        }
        public static UInt32 IsKeyboardVisible()
        {
            IntPtr keyboardHandle = GetKeyboardWindowHandle();

            bool visible = false;
            UInt32 style = 0;
            if (keyboardHandle != IntPtr.Zero)
            {
                style = GetWindowLong(keyboardHandle, GWL_STYLE);
                visible = ((style & WS_DISABLED) != WS_DISABLED);
            }

            return style;
        }
        public static void InputEventFun(object sender, EventArgs e)
        {
            if (sender is Control)
            {
                Control ct = sender as Control;
                if (ct is ComboBox)
                {
                    ComboBox cb = ct as ComboBox;
                    if (cb.Focused)
                    {
                        if (!cb.DroppedDown)
                            TabTipHelper.ShowScreenKeyboard();
                        else if (cb.Items.Count == 0)
                            TabTipHelper.ShowScreenKeyboard();
                    }
                }
                else if (ct.Focused)
                {
                    TabTipHelper.ShowScreenKeyboard();
                }
            }
        }

        /// <summary>
        /// 添加输入事件
        /// </summary>
        public static void AddInputEvents(object sender,EventHandler Input)
        {
            if (sender is Control)
            {
                Control csender = sender as Control;
                foreach (Control control in csender.Controls)
                {

                    if (control.Controls.Count > 0)
                    {
                        AddInputEvents(control, Input);
                    }
                    else
                    {
                        if (control is TextBox)
                        {
                            //if (control.Tag is string)
                            try
                            {
                                if (control.Tag.ToString() != "null" && control.Tag.ToString() != "")
                                {
                                    TextBox tb = control as TextBox;
                                    if (!tb.ReadOnly)
                                    {
                                        tb.BackColor = Color.White;
                                        tb.ForeColor = Color.Black;
                                        tb.ReadOnly = true;
                                        tb.Click += Input;                                      
                                    }
                                }
                                else
                                { 
                                }
                            }
                            catch
                            {
 
                            }
                        }
                        //else if (control is ComboBox)
                        //{
                        //    ComboBox cb = control as ComboBox;
                        //    if (cb.DropDownStyle != ComboBoxStyle.DropDownList)
                        //    {
                        //        cb.Click += Input;
                        //    }
                        //}
                    }
                }
            }
        }
    }

    /// <summary>
    /// 输入事件
    /// </summary>
    /// <param name="InputObject"></param>
    public delegate void BHXJInputEvent(object InputObject);
}
